import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.yarn.server.nodemanager.Context;

import java.io.IOException;

public class PaiXu {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        System.out.println(args[0]);
        System.out.println(args[1]);
        Configuration conf =  new Configuration();
        Job job =Job.getInstance(conf,"罗斯福好吃");
        job.setJarByClass(PaiXu.class);                             //运行本身这类
        job.setMapperClass(LsfMapper.class);                //指定Map的类
        job.setReducerClass(LsfReducer.class);                //指定Reducer的类
        job.setOutputKeyClass(IntWritable.class);                    //指定map输入key类型
        job.setOutputValueClass(Text.class);  //                    //指定map输出value类型
        FileInputFormat.addInputPath(job, new Path(args[0]));    //输入位置路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));  //输出位置路径
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }
    public static class LsfMapper
            extends Mapper<Object,Text, IntWritable,Text >{

        @Override
        protected void map(Object key,Text value,Mapper<Object,Text, IntWritable,Text>.Context context
        ) throws IOException, InterruptedException {
            int sales = Integer.parseInt(value.toString().split(", ")[1]);
            context.write(new IntWritable(sales),value);

        }
    }

    public static class LsfReducer extends Reducer<IntWritable,Text,NullWritable,Text> {
        @Override
        protected void reduce(IntWritable key, Iterable<Text>values, Reducer<IntWritable,Text,NullWritable,Text>.Context context) throws IOException ,InterruptedException {
            for (Text t:values){
                context.write(NullWritable.get(),t);
            }

        }

}}
